//
//  MyAccountViewController.m
//  PlayerWorld
//
//  Created by 1 on 16/4/28.
//  Copyright © 2016年 刘成. All rights reserved.
//
#define UISCREEN_WIDTH  [UIScreen mainScreen].bounds.size.width
#define UISCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#import "MyAccountViewController.h"
#import "MyAccountTableViewCell.h"
#import "PersonalViewController.h"
#import "AccountDetailsViewController.h"
#import "QuickRechargeViewController.h"
#import "LightningLiftViewController.h"
#import "BettingRecordViewController.h"
#import "TrackRecordViewController.h"
#import "AccountDetailsViewController.h"
#import "AnnouncementmaiViewController.h"

#import "HighLightsModel.h"
#import "UIImageView+WebCache.h"
#import "KaihuViewController.h"
@interface MyAccountViewController ()

@end

@implementation MyAccountViewController
{
    HighLightsModel *model;

}

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor greenColor];
    UILabel *atitlelabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 120, 30)];
    atitlelabel.text = @"我 的 账 户";
    atitlelabel.textAlignment = NSTextAlignmentCenter;
    self.navigationController.navigationBar.translucent = NO;
    atitlelabel.font = [UIFont boldSystemFontOfSize:17];
    atitlelabel.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = atitlelabel;
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@""style:UIBarButtonItemStylePlain target:self action:nil];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:36/255.0f green:11/255.0f blue:54/255.0f alpha:1];
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

    self.myAccounttitles1 = @[@"投注记录",@"追号记录",@"账户明细"];
    self.myAccountimageNames1 = @[@"icon_user_g",@"icon_rec",@"icon_cup"];
    
    self.myAccounttitles2 = @[@"我的站内信",@"开户中心",@"账户设置",];
    self.myAccountimageNames2 = @[@"icon_message",@"开户中心",@"icon_setting"];
    
    MyAccounttableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, UISCREEN_HEIGHT) style:UITableViewStylePlain];
    MyAccounttableView.dataSource = self;
    MyAccounttableView.delegate = self;
    [MyAccounttableView registerNib:[UINib nibWithNibName:@"HighLightsTableViewCell" bundle:nil]  forCellReuseIdentifier:@"cell"];
    MyAccounttableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:MyAccounttableView];
    
    [self myaccountheadtried];
}
// 我的账户头视图
-(void)myaccountheadtried {
    // 我的账户头视图
    UIImageView *MyAccountHeaderImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, 160)];
    MyAccountHeaderImageView.userInteractionEnabled=YES;
    [MyAccountHeaderImageView setImage:[UIImage imageNamed:@"account_bg.jpg"]];
    
    // 设置按钮
    UIButton *setUptbutton = [[UIButton alloc]initWithFrame:CGRectMake(UISCREEN_WIDTH-10-30, 20, 30, 30)];
    [setUptbutton addTarget:self action:@selector(setUptbutton:) forControlEvents:UIControlEventTouchUpInside];
    [setUptbutton setImage:[UIImage imageNamed:@"icon_setting"] forState:UIControlStateNormal];
    [setUptbutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [setUptbutton setBackgroundColor:[UIColor clearColor]];
    [MyAccountHeaderImageView addSubview:setUptbutton];

    // 从NSUserDefaults中取出数据
    model = [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] valueForKey:@"uid"]];
    // 账户头像
    UIImageView *myAccountImageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 50, 45, 45)];
    [myAccountImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://666.lefun6.com/%@",model.U_Head]]];
//    [myAccountImageView setBackgroundColor:[UIColor redColor]];
    [MyAccountHeaderImageView addSubview:myAccountImageView];
    
    // 账户名label
    UILabel *MyAccountlabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(myAccountImageView.frame)+10, myAccountImageView.frame.origin.y, 150, 20)];
    MyAccountlabel.text = model.U_UserName;
    MyAccountlabel.font = [UIFont systemFontOfSize: 16.0];
    MyAccountlabel.textColor = [UIColor whiteColor];
    MyAccountlabel.backgroundColor = [UIColor clearColor];
    [MyAccountHeaderImageView addSubview:MyAccountlabel];
    
    // 余额label
    UILabel *MyAccountbalancelabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(myAccountImageView.frame)+10, CGRectGetMaxY(MyAccountlabel.frame)+5, 150, 20)];
    MyAccountbalancelabel.text = [NSString stringWithFormat:@"余额：%@ 元",model.U_Money];
    MyAccountbalancelabel.font = [UIFont systemFontOfSize: 16.0];
    MyAccountbalancelabel.textColor = [UIColor whiteColor];
    MyAccountbalancelabel.backgroundColor = [UIColor clearColor];
    [MyAccountHeaderImageView addSubview:MyAccountbalancelabel];
    
    // 账户详情
    UIImageView *accountDetailsImageView = [[UIImageView alloc] initWithFrame:CGRectMake(UISCREEN_WIDTH-10-20, (MyAccountHeaderImageView.frame.size.height-15)/2, 10, 20)];
    [accountDetailsImageView setImage:[UIImage imageNamed:@"icon_arrow"]];
    [MyAccountHeaderImageView addSubview:accountDetailsImageView];
    
    // 分割线label
    UILabel *myAccountrulelabel = [[UILabel alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(myAccountImageView.frame)+20, UISCREEN_WIDTH-20-20, 1)];
    myAccountrulelabel.backgroundColor = [UIColor grayColor];
    [MyAccountHeaderImageView addSubview:myAccountrulelabel];
    
    //快速充值
    UIButton *quickRechargebutton = [[UIButton alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(myAccountrulelabel.frame), UISCREEN_WIDTH/2, CGRectGetMaxY(MyAccountHeaderImageView.frame)-CGRectGetMaxY(myAccountrulelabel.frame))];
    [quickRechargebutton addTarget:self action:@selector(quickRechargebuttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [quickRechargebutton setImage:[UIImage imageNamed:@"icon_card"] forState:UIControlStateNormal];
    [quickRechargebutton setTitle:@"快速充值" forState:UIControlStateNormal];
    quickRechargebutton.titleLabel.font = [UIFont systemFontOfSize: 16.0];
    [quickRechargebutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [quickRechargebutton setBackgroundColor:[UIColor clearColor]];
    [MyAccountHeaderImageView addSubview:quickRechargebutton];
    
    // button分割线label
    UILabel *buttonLinelabel = [[UILabel alloc]initWithFrame:CGRectMake(UISCREEN_WIDTH/2, CGRectGetMaxY(myAccountrulelabel.frame)+5, 1, CGRectGetMaxY(MyAccountHeaderImageView.frame)-CGRectGetMaxY(myAccountrulelabel.frame)-5-5)];
    buttonLinelabel.backgroundColor = [UIColor grayColor];
    [MyAccountHeaderImageView addSubview:buttonLinelabel];
    
    // 闪电提现
    UIButton *lightningLiftbutton = [[UIButton alloc]initWithFrame:CGRectMake(UISCREEN_WIDTH/2, CGRectGetMaxY(myAccountrulelabel.frame), UISCREEN_WIDTH/2, CGRectGetMaxY(MyAccountHeaderImageView.frame)-CGRectGetMaxY(myAccountrulelabel.frame))];
    [lightningLiftbutton addTarget:self action:@selector(lightningLiftbuttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [lightningLiftbutton setImage:[UIImage imageNamed:@"icon_code"] forState:UIControlStateNormal];
    [lightningLiftbutton setTitle:@"闪电提现" forState:UIControlStateNormal];
    lightningLiftbutton.titleLabel.font = [UIFont systemFontOfSize: 16.0];
    [lightningLiftbutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [lightningLiftbutton setBackgroundColor:[UIColor clearColor]];
    [MyAccountHeaderImageView addSubview:lightningLiftbutton];
    
    MyAccounttableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 160)];
    UITapGestureRecognizer *tapGeture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(ActionDo:)];
    
    [MyAccounttableView.tableHeaderView addGestureRecognizer:tapGeture];
    [MyAccounttableView.tableHeaderView addSubview:MyAccountHeaderImageView];

}
// 我的账户头试图中的个人设置按钮点击事件
- (void)setUptbutton:(UIButton *)sender {
    PersonalViewController *pVC= [[PersonalViewController alloc] init];
    [self.navigationController pushViewController:pVC animated:YES];
}
// 点击头试图事件
- (void)ActionDo:(UIButton *)sender {
    AccountDetailsViewController *aDVC= [[AccountDetailsViewController alloc] init];
    [self.navigationController pushViewController:aDVC animated:YES];
}
// 快速充值按钮点击事件
- (void)quickRechargebuttonAction:(UIButton *)sender {

    UIAlertController *alerView = [UIAlertController alertControllerWithTitle:@"提示信息" message:@"模块暂未开通，敬请期待" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

    }];
    [alerView addAction:action];
    [self presentViewController:alerView animated:YES completion:nil];
//    QuickRechargeViewController *quickRechargeVC= [[QuickRechargeViewController alloc] init];
//    [self.navigationController pushViewController:quickRechargeVC animated:YES];
}
// 闪电提现按钮点击事件
- (void)lightningLiftbuttonAction:(UIButton *)sender {
    
    UIAlertController *alerView = [UIAlertController alertControllerWithTitle:@"提示信息" message:@"模块暂未开通，敬请期待" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

    }];
    [alerView addAction:action];
    [self presentViewController:alerView animated:YES completion:nil];

//    LightningLiftViewController *lightningLiftVC= [[LightningLiftViewController alloc] init];
//    [self.navigationController pushViewController:lightningLiftVC animated:YES];
}

- (CGFloat)tableView:(UITableView *)atableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 45;
}


//设置表视图中的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
    return [_myAccountimageNames1 count];
    } else {
    return [_myAccountimageNames2 count];
    }
}
//设置表视图中的section（分组）数,默认为1
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}
// 设置header的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20;
}

//设置单元格中的数据
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIdent = @"MyAccountTableViewCell";
    MyAccountTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdent];
    if (cell == nil) {
        cell = [[MyAccountTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdent];
    }
    cell.backgroundColor = [UIColor whiteColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.font = [UIFont systemFontOfSize:16];
    cell.textLabel.textColor = [UIColor grayColor];
 
    
    //设置数据
    if (indexPath.section == 0) {
        cell.textLabel.text = _myAccounttitles1[indexPath.row];
        cell.imageView.image = [UIImage imageNamed:_myAccountimageNames1[indexPath.row]];
    }else{
        cell.textLabel.text = _myAccounttitles2[indexPath.row];
        cell.imageView.image = [UIImage imageNamed:_myAccountimageNames2[indexPath.row]];
    }
    return cell;
}
//单元格点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0 & indexPath.row == 0) {
    BettingRecordViewController *bettingRecordVC= [[BettingRecordViewController alloc] init];
    [self.navigationController pushViewController:bettingRecordVC animated:YES];
     }else if (indexPath.section == 0 & indexPath.row == 1){
    TrackRecordViewController *trackRecordVC= [[TrackRecordViewController alloc] init];
    [self.navigationController pushViewController:trackRecordVC animated:YES];
     }else if (indexPath.section == 0 & indexPath.row == 2){
    AccountDetailsViewController *accountDetailsVC= [[AccountDetailsViewController alloc] init];
    [self.navigationController pushViewController:accountDetailsVC animated:YES];
     }else if (indexPath.section == 1 & indexPath.row == 0){
         
     UIAlertController *alerView = [UIAlertController alertControllerWithTitle:@"提示信息" message:@"模块暂未开通，敬请期待" preferredStyle:UIAlertControllerStyleAlert];
     UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

     }];
     [alerView addAction:action];
     [self presentViewController:alerView animated:YES completion:nil];
     }
     else if (indexPath.section == 1 && indexPath.row == 1){
         KaihuViewController *kaihuView = [[KaihuViewController alloc] init];
         [kaihuView setHidesBottomBarWhenPushed:YES];
         [self.navigationController pushViewController:kaihuView animated:YES];
     }
     else{
    PersonalViewController *personalVC= [[PersonalViewController alloc] init];
    [self.navigationController pushViewController:personalVC animated:YES];
     }
}
@end
